import axios from "axios";
import store from "../../store";
import {Message} from "element-ui";
import router from "../../router"
import {SyntaxError} from "core-js/internals/dom-exception-constants";
import {Toast} from "vant";

axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.get['Content-Type'] = 'application/json';

const instance = axios.create({
    baseURL: '/tbb',
    // timeout: 1000,
    headers: {},
});
instance.interceptors.request.use(
    config => {
        const token = store.state.token;
				console.log('token', token)
        if (token) {
            config.headers.accessToken = token;
        }
        return config;
    },
    error => {
        return Promise.error(error);
    },
)

instance.interceptors.response.use(
    function (response) {
        if (Boolean(response.data.status)) {
            console.log();
            return response;
        } else {
            if (response.config.url.startsWith("/api/lzs/")) {
                Message.error(response.data.message);
            } else {
                Toast(response.data.message);
            }
            if (response.data.code === "00001") {
                // 操作失败
            } else if (response.data.code === "00002") {
                // 登录过期
                router.push("/login");
            } else if (response.data.code === "00003") {
                // 参数校验
            }
            return Promise.reject(response.data)
        }
    },
    function (error) {
        // Message.error(error.message);
        return Promise.reject(error);
    }
)

export default instance